package com.zhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.mapper.DeptMapper;
import com.zhu.pojo.Dept;
import com.zhu.query.DeptQuery;
import com.zhu.service.IDeptService;
import com.zhu.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaozhu
 * @since 2022-08-24
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<DeptVo> getDept(DeptQuery deptQuery) {
        List<DeptVo> deptVoList = deptMapper.getDept(deptQuery.getDid(), deptQuery.getDeptName(), deptQuery.getStatus());
        List<DeptVo> deptVos = deptVoList;
        for (DeptVo deptVo:deptVoList){
            deptVos = deptVos.stream().filter(v->v.getPid()!=deptVo.getDid()).collect(Collectors.toList());
        }
        return deptVos;
    }

    @Override
    public void removeTree(List<Long> idList) {
        //获取该id下的所有子数据的id
        selectChildren(idList.get(0),idList);
        System.out.println(idList);
        //将所有的子数据以及本身都删除
//        baseMapper.deleteBatchIds(idList);
    }

    public void selectChildren(Long did,List<Long> idList){
        //根据pid获取到下等级的子数据
        List<Dept> deptList = baseMapper.selectList(new QueryWrapper<Dept>().eq("pid", did).select("did"));
        //遍历子数据，将id添加到集合当中，并且递归的获取
        deptList.forEach(item->{
            idList.add(item.getDid());
            selectChildren(item.getDid(),idList);
        });
    }


}
